package org.javaforever.gatescore.shiroauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontCodeBlock;
import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontMethod;
import org.javaforever.gatescore.core.FrontVerb;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.exception.ValidateException;

public class ListActive extends FrontVerb {
	private static final long serialVersionUID = -7040702252039560054L;
	protected boolean profileController = false;

	public boolean isProfileController() {
		return profileController;
	}

	public void setProfileController(boolean profileController) {
		this.profileController = profileController;
	}

	public ListActive(FrontDomain d) throws ValidateException{
		super(d);
		this.denied = domain.isVerbDenied("ListActive");
		this.setVerbName("ListActive" + d.getCapFirstPlural());
	}

	@Override
	public FrontMethod generateControllerMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			FrontMethod method = new FrontMethod();
			method.setStandardName("listActive" + this.domain.getCapFirstPlural());
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L, 2, "listActive" + this.domain.getCapFirstPlural() + "() {"));
			sList.add(new Statement(2000L, 3, "listActive" + this.domain.getCapFirstPlural() + "().then("));
			sList.add(new Statement(3000L, 4, "response => {"));
			sList.add(
					new Statement(4000L, 5, "this.active" + this.domain.getCapFirstPlural() + " = response.data.rows"));
			sList.add(new Statement(5000L, 4, "})"));
			sList.add(new Statement(6000L, 2, "},"));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public FrontMethod generateApiMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			FrontMethod method = new FrontMethod();
			method.setStandardName("listActive" + this.domain.getCapFirstPlural());
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L, 0, "export function listActive" + this.domain.getCapFirstPlural() + "() {"));
			sList.add(new Statement(2000L, 1, "return request({"));
			if (this.isProfileController()) {
				sList.add(new Statement(3000L, 2,
						"url: 'profile" + this.domain.getControllerNamingSuffix()
								+ "/listActive" + this.domain.getCapFirstPlural() + "',"));
			} else if (!this.isProfileController() && this.domain.isUseController()) {
				sList.add(new Statement(3000L, 2,
						"url: 'login" + this.domain.getControllerNamingSuffix()
								+ "/listActive" + this.domain.getCapFirstPlural() + "',"));
			} else {
				sList.add(new Statement(3000L, 2, "url: '" + this.domain.getLowerFirstDomainName() + "Facade/listActive"
						+ this.domain.getCapFirstPlural() + "',"));
			}
			sList.add(new Statement(4000L, 2, "method: 'post'"));
			sList.add(new Statement(5000L, 1, "})"));
			sList.add(new Statement(6000L, 0, "}"));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public FrontCodeBlock generateRouteBlockBlock() throws Exception {
		return null;
	}

	@Override
	public FrontMethod generateControllerMethod(String indexSuffix) throws Exception {
		return generateControllerMethod();
	}

}
